﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class MaterialOrderAdd : BasePage
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
        TestPurview(HttpContext.Current, 1);
        if (!Page.IsPostBack)
        {

            BindUserList();
            string strMatId = Request.QueryString["matId"];
            if (!Common.Help.IsNumber(strMatId))
            {
                this.labHeader.Text = "添加";
                this.rdBtnRight.SelectedValue = "0";         



            }
            else
            {
                this.labHeader.Text = "修改";
                this.btnSave.Text = "修改";
                this.btnRelease.Enabled = true;
                int matId = int.Parse(strMatId);
                Model.SessionUser sUser = this.getSessionUser(HttpContext.Current);
                BLL.MaterialOrderBLL bll = new BLL.MaterialOrderBLL();

                Model.materialOrder model = bll.GetModel(matId, sUser.uid);
                if (model == null)
                {
                    Response.Redirect("~/error.aspx");
                    return;
                }
                
                this.txtId.Value = matId.ToString();
                this.txtAmont.Text = model.amount.ToString();
                this.txtComment.Text = model.amount.ToString();
                this.txtDeliveryDate.Text =((DateTime) model.deliveryDate).ToShortDateString();
                this.txtEndDate.Text = ((DateTime)model.endDate).ToShortDateString();  
                this.txtName.Text = model.name.ToString();
                this.txtSpec.Text = model.spec.ToString();
                this.rdBtnRight.SelectedValue = model.rightType.ToString();

                this.userIds.Text = bll.GetHaveRightUserIds(matId);

            }
            
        }
    }
    private void BindUserList()
    {
        BLL.UserBLL bll = new BLL.UserBLL();
        this.lstAll.DataSource = bll.GetListByType(2); //得到所有原料供应商
        this.lstAll.DataBind();
    }
   
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int matId = int.Parse(this.txtId.Value);
        string strIds = this.userIds.Text;
        string strName = this.txtName.Text;
        if (strName == "")
        {
            Common.Jscript.Alert("名称不能为空！");
            return;
        }
        string strSpec = this.txtSpec.Text;
        if (strSpec == "")
        {
            Common.Jscript.Alert("规格不能为空！");
            return;
        }
        string strAmount = this.txtAmont.Text;
        if (!Common.Help.IsNumber(strAmount))
        {
            Common.Jscript.Alert("数量必须为数字！");
            return;
        }
        string strEndDate = this.txtEndDate.Text;

        if (!Common.Help.IsDate(strEndDate))
        {
            Common.Jscript.Alert("请确认结束日期格式正确！");
            return;
        }
        DateTime endDate = DateTime.Parse(strEndDate);
        if (endDate < DateTime.Now)
        {
            Common.Jscript.Alert("结束日期必须大于当前日期！");
            return;
        }
        string strDeliveryDate = this.txtDeliveryDate.Text;

        if (!Common.Help.IsDate(strDeliveryDate))
        {
            Common.Jscript.Alert("请确认发货日期格式正确！");
            return;
        }
        DateTime DeliveryDate = DateTime.Parse(strDeliveryDate);
        if (DeliveryDate < DateTime.Now)
        {
            Common.Jscript.Alert("发货日期必须大于当前日期！");
            return;
        }
        string strComment = this.txtComment.Text;
        string strRightType = this.rdBtnRight.SelectedValue;
        if (strRightType == "1" && strIds == "")
        {
            Common.Jscript.Alert("如果设置查看权限为指定客户，请至少选择一个客户！");
            return;
        }

        BLL.MaterialOrderBLL bll = new BLL.MaterialOrderBLL();

        Model.materialOrder model = new Model.materialOrder();
        model.amount = double.Parse(strAmount);
        model.comment = strComment;
        Model.SessionUser sUser = this.getSessionUser(HttpContext.Current);
        model.creatorID = sUser.uid;
        model.deliveryDate = DeliveryDate;
        model.endDate = endDate;
        model.name = strName;
        model.rightType = int.Parse(strRightType);
        model.spec = strSpec;
        model.rightUserList = strIds;
        if (matId > 0) //修改
        {
            model.matId = matId;
            if(bll.Update(model))
            {
                Common.Jscript.Alert("保存成功！");
            }
        }
        else //增加
        {
            if (bll.Add(model))
            {
                Common.Jscript.Alert("保存成功！");
                this.btnSave.Text = "修改";
                this.btnRelease.Enabled = true;
            }


        }
    }
}
